﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EHTWebsiteProject.Code.Entity;
using EHTWebsite.Code;

namespace EHTWebsiteProject.Administrator
{
    public partial class AdminCustomerCategoryIU : System.Web.UI.Page
    {
        public Int32 danhMucKhachHangID = -1;
       // private String message = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    danhMucKhachHangID = Int32.Parse(Request.QueryString["DanhMucKhachHangID"].ToString());
                }
                catch(Exception ex)
                {
                    danhMucKhachHangID = -1;
                }
                if (danhMucKhachHangID == -1)
                {
                    this.lbAdminNewCategory.Text = "Thêm mới danh mục khách hàng";
                    ViewState["action"] = HangSo.ACTION_ADD;
                }
                else
                {
                    this.lbAdminNewCategory.Text = "Update danh mục khách hàng";
                    DanhMucKhachhang theDanhmuc = DanhMucKhachHangProvider.GetDanhMucKhachHangByDanhMucID(danhMucKhachHangID);
                    if (theDanhmuc != null)
                    {
                        this.txtTenDanhMucTinTucVN.Text=theDanhmuc.TenDanhMucKhachHangVN;
                        this.txtTenDanhMucTinTucOther.Text = theDanhmuc.TenDanhMucKhachHangOther;
                        this.ckPublish.Checked = theDanhmuc.Publish;
                        this.txtSapXep.Text = theDanhmuc.NumberOfOrder.ToString();
                        ViewState["DanhMucKhachHangID"] = theDanhmuc.DanhMucKhachHangID;
                        ViewState["action"] = HangSo.ACTION_UPDATE;
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String action = ViewState["action"].ToString();
            DanhMucKhachhang theDanhmuc = new DanhMucKhachhang();
            theDanhmuc.TenDanhMucKhachHangVN = this.txtTenDanhMucTinTucVN.Text;
            theDanhmuc.TenDanhMucKhachHangOther = this.txtTenDanhMucTinTucOther.Text;
            theDanhmuc.Publish = this.ckPublish.Checked;

            try
            {
                theDanhmuc.NumberOfOrder = Int32.Parse(this.txtSapXep.Text.ToString());
            }
            catch(Exception ex)
            {
                theDanhmuc.NumberOfOrder = 1;
                this.txtSapXep.Text = "1";
            }
            if (action == HangSo.ACTION_ADD)
            {
                theDanhmuc.DanhMucKhachHangID = -1;
                DanhMucKhachHangProvider.IUDanhMucKhachHangs(theDanhmuc, true);
            }
            else
            {
                theDanhmuc.DanhMucKhachHangID = Int32.Parse(ViewState["DanhMucKhachHangID"].ToString());
                DanhMucKhachHangProvider.IUDanhMucKhachHangs(theDanhmuc,false);
            }
            Response.Redirect("AdminCustomersCategorys.aspx");
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            String action = ViewState["action"].ToString();
            DanhMucKhachhang theDanhmuc = new DanhMucKhachhang();
            theDanhmuc.TenDanhMucKhachHangVN = this.txtTenDanhMucTinTucVN.Text;
            theDanhmuc.TenDanhMucKhachHangOther = this.txtTenDanhMucTinTucOther.Text;
            theDanhmuc.Publish = this.ckPublish.Checked;
            theDanhmuc.NhomDanhMucID = -1;
            try
            {
                theDanhmuc.NumberOfOrder = Int32.Parse(this.txtSapXep.Text.ToString());
            }
            catch (Exception ex)
            {
                theDanhmuc.NumberOfOrder = 1;
                this.txtSapXep.Text = "1";
            }
            if (action == HangSo.ACTION_ADD)
            {
                theDanhmuc.DanhMucKhachHangID = -1;
                DanhMucKhachHangProvider.IUDanhMucKhachHangs(theDanhmuc, true);
            }
            else
            {
                theDanhmuc.DanhMucKhachHangID = Int32.Parse(ViewState["DanhMucKhachHangID"].ToString());
                DanhMucKhachHangProvider.IUDanhMucKhachHangs(theDanhmuc, false);
            }

            //Xuất thông báo lưu thành công
            this.lbMessage.CssClass = "BlueCorlor";
            this.lbMessage.Text = "Lưu thành công";

            Response.Redirect("AdminCustomersCategorys.aspx");
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminCustomersCategorys.aspx");
        }
    }
}